package usian.hx.login.service.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.common.pojo.ResultVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import usian.hx.login.dao.LoginDao;
import usian.hx.login.dao.UserDao;
import usian.hx.login.pojo.TbUser;
import usian.hx.login.service.LoginService;

/**
 * @author Ai
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginDao loginDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private UserDao userDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultVo sendCode(String phone) {
        //判断手机号正确
        Boolean  isPhone= Validator.isMobile(phone);
        if (isPhone){
            //判断手机号注册
            int count= loginDao.getUserCountByPhone(phone);
            if (count>0){
                //手机号已经注册
                //发送短信验证码
                rabbitTemplate.convertAndSend("java-exchange","java-queue",phone);

                return new ResultVo(200,"短信发送成功",null);
            }else {
                return new ResultVo(303,"手机号码错误",null);
            }
        }else {
            return new ResultVo(303,"手机号码错误",null);
        }
    }

    @Override
    public ResultVo userLogin(String phone, String code) {
        Boolean isHavephone= StrUtil.isEmpty(phone);
        boolean isHaveCode = StringUtils.isEmpty(code);
        if (!isHavephone){
            if (!isHaveCode){
                LambdaQueryWrapper<TbUser> lambdaQueryWrapper=new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(TbUser::getMobile,phone);
                TbUser  tbUser=userDao.selectOne(lambdaQueryWrapper);
                if (tbUser!=null){
                    String redisCode=stringRedisTemplate.opsForValue().get(phone);
                    if (!StringUtils.isEmpty(redisCode)){
                        if (redisCode.equals(code)){
                            return new ResultVo(200,"success",null);
                        }else {
                            return new ResultVo(303,"验证码输入错误",null);

                        }
                    }else {
                        return new ResultVo(303,"验证码过期",null);

                    }
                }else {
                    return new ResultVo(303,"用户不存在",null);
                }
            }else {
                return new ResultVo(303,"验证码必须填写",null);
            }
        }
        else {
            return new ResultVo(303,"手机号码必须填写",null);
        }
    }

    @Override
    public ResultVo save(TbUser tbUser) {
        userDao.insert(tbUser);
        return new ResultVo(200,"success",null);

    }
}
